package com.idbk.chargestation.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.idbk.chargestation.R;
import com.idbk.chargestation.bean.JsonMyBookingItem;

import java.util.List;
import java.util.Locale;

/**
 * 预约历史适配器
 * @author zhx
 */
public class BookHistoryAdapter extends BaseAdapter {

    private Context mContext;
    private List<JsonMyBookingItem> mList;
    private LayoutInflater mInflater;

    public BookHistoryAdapter(Context context, List<JsonMyBookingItem> list) {
        mList = list;
        mInflater = LayoutInflater.from(context);
        mContext = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View view;
        BookHistoryHold hold;
        if (convertView == null) {
            view = mInflater.inflate(R.layout.item_book_history, parent, false);
            hold = new BookHistoryHold();
            hold.initView(view);
            view.setTag(hold);
        } else {
            view = convertView;
            hold = (BookHistoryHold) view.getTag();
        }

        hold.fillDate(mList.get(position));

        return view;
    }

    private class BookHistoryHold {
        private TextView mPile, mEndTime, mCost, mStartTime, mPileName;
        private ImageView mImgState;

        private void initView(View parent) {
            mPile = (TextView) parent.findViewById(R.id.textview_pile);
            mEndTime = (TextView) parent.findViewById(R.id.textview_endtime);
            mCost = (TextView) parent.findViewById(R.id.textview_cost);
            mStartTime = (TextView) parent.findViewById(R.id.textview_starttime);
            mPileName = (TextView) parent.findViewById(R.id.textview_east);
            mImgState = (ImageView) parent.findViewById(R.id.image_state);
        }

        private void fillDate(JsonMyBookingItem data) {
            String text;
            if (data.bespeakStatue == JsonMyBookingItem.BESPEAK_STATUS_IN) {
                text = data.stopTime + " 预约中";
                mStartTime.setText(text);
                mImgState.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.bg_me_booking_countdown));
            } else if (data.bespeakStatue == JsonMyBookingItem.BESPEAK_STATUS_CANCEL_SELF) {
                text = data.stopTime + " 取消预约";
                mStartTime.setText(text);
                mImgState.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.bg_me_booking_cancelled));
            } else if (data.bespeakStatue == JsonMyBookingItem.BESPEAK_STATUS_CANCEL_TIMEOUT) {
                text = data.stopTime + " 超时自动取消";
                mStartTime.setText(text);
                mImgState.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.bg_me_booking_cancelled));
            } else if (data.bespeakStatue == JsonMyBookingItem.BESPEAK_STATUS_FINISH) {
                text = data.stopTime + " 赴约充电";
                mStartTime.setText(text);
                mImgState.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.bg_me_booking_finished));
            } else {
                text = data.stopTime + " 取消预约";
                mStartTime.setText(text);
                mImgState.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.bg_me_booking_cancelled));
            }
            mPileName.setText(data.pointName);
            String pile = "预约电桩: " + String.format(Locale.CHINA, "#%02d", data.pileNum);
            mPile.setText(pile);
            String time = "充电时间: " + data.getSimpleEndTime();
            mEndTime.setText(time);
            mCost.setText("预约费用:  " + data.expenseMoney + "元");

        }

    }
}
